/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.course.subscribe;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import cn.com.chinasoa.course.subscribe.model.PrivilegeStrategy;
import cn.com.chinasoa.course.subscribe.util.EntityManagerHelper;

import com.x4ee.soa.portal.framework.core.ActionConfig;
import com.x4ee.soa.portal.framework.core.ActionFilter;
import com.x4ee.soa.portal.framework.core.BaseRequestFilter;

/**
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-11-2上午01:27:11
 */
public class PrivilegeStrategyCreateActionFilter extends BaseRequestFilter
		implements ActionFilter {

	private static String SUCCESS = "success";
	private static String FAILED = "error";

	public String process(ActionRequest actionRequest, ActionResponse actionResponse, ActionConfig actionConfig) 
		throws PortletException, IOException {
		
		String description = actionRequest.getParameter("description");
		String discount = actionRequest.getParameter("discount");

		boolean success = false;
		if((description!=null)&&(discount!=null)){
			int discountInt= -1;
			try{discountInt=Integer.parseInt(discount);
			}catch(Exception e){}
			if((discountInt>0)&&(discountInt<=100)){
				EntityManager em = EntityManagerHelper.getEntityManager();
				em.getTransaction().begin();
				PrivilegeStrategy vPrivilegeStrategy = new PrivilegeStrategy();
				vPrivilegeStrategy.setDescription(description);
				vPrivilegeStrategy.setDiscount(discountInt);
				em.persist(vPrivilegeStrategy);
				em.getTransaction().commit();
				EntityManagerHelper.closeEntityManager();
				success = true;
			}else{
				success = false;
			}
		}else{
			success = false;
		}
		if(success)return SUCCESS;
		else return FAILED;
	}

}
